package com.ruoyi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.IPage;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.ProjectItem;
import com.ruoyi.common.entity.dto.ProjectItemPageDto;
import com.ruoyi.common.entity.dto.ResourceItemDto;
import com.ruoyi.common.service.ProjectItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/project-items")
public class ProjectItemController {

    @Autowired
    private ProjectItemService projectItemService;

    @GetMapping("/items")
    public R<Page<ProjectItem>> getProjects(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {

        Page<ProjectItem> projectPage = new Page<>(page, size);

        LambdaQueryWrapper<ProjectItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(ProjectItem::getCreateTime);
        queryWrapper.orderByDesc(ProjectItem::getId);
        queryWrapper.eq(ProjectItem::getStatus, 1);
        projectItemService.page(projectPage, queryWrapper);

        return R.ok(projectPage);
    }

    // 根据项目ID获取课程项列表 (示例：分页查询)
    @GetMapping
    public R<Page<ProjectItem>> getProjectItemsByProjectId(
            @RequestParam Long projectId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "100") Integer size) { // 假设目录项不多，设置较大默认值
        Page<ProjectItem> itemPage = new Page<>(page, size);
        LambdaQueryWrapper<ProjectItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectItem::getProjectId, projectId);
        queryWrapper.orderByAsc(ProjectItem::getSortOrder); // 按排序字段升序
        projectItemService.page(itemPage, queryWrapper);
        return R.ok(itemPage);
    }

    // 根据ID获取课程项详情
    @GetMapping("/{id}")
    public R<ProjectItem> getProjectItemById(@PathVariable Long id) {
        ProjectItem item = projectItemService.getById(id);
        if (item != null) {
            return R.ok(item);
        } else {
            return R.fail("课程项未找到");
        }
    }

    // 创建课程项
    @PostMapping
    public R<ProjectItem> createProjectItem(@RequestBody ProjectItem item) {
        // 这里可以添加创建人等逻辑
        // item.setCreateBy("current_user"); // 示例
        boolean saved = projectItemService.save(item);
        if (saved) {
            return R.ok(projectItemService.getById(item.getId())); // 返回完整信息
        } else {
            return R.fail("创建课程项失败");
        }
    }

    // 更新课程项
    @PutMapping("/{id}")
    public R<String> updateProjectItem(@PathVariable Long id, @RequestBody ProjectItem item) {
        item.setId(id); // 确保ID一致
        // 这里可以添加更新人等逻辑
        // item.setUpdateBy("current_user"); // 示例
        boolean updated = projectItemService.updateById(item);
        if (updated) {
            return R.ok("更新成功");
        } else {
            return R.fail("更新失败");
        }
    }

    // 删除课程项
    @DeleteMapping("/{id}")
    public R<String> deleteProjectItem(@PathVariable Long id) {
        boolean removed = projectItemService.removeById(id);
        if (removed) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }
    @PostMapping("/all")
    public  Map<String, Object> getAllProjectItems(@RequestBody(required = false) ProjectItemPageDto queryDto) {
        Map<String, Object> allProjectItems = projectItemService.getAllProjectItems(queryDto);

        return allProjectItems;

    }
    /*@PostMapping("/all")
    public Page getAllProjectItems(@RequestBody(required = false) ProjectItem item) {
        Map<String, List<ResourceItemDto>> allProjectGroupedByDate = projectItemService.getAllProjectGroupedByDate(item);
        return R.ok(allProjectGroupedByDate);
    }*/
}